package dao

import (
	"errors"
	"github.com/GUOSAITONG/MyConfig/pkg"
	"hospital-srv/basic/inits"
	__ "hospital-srv/basic/proto"
	"math/rand"
	"strconv"
	"time"
)

func SendSms(in *__.SendSmsReq) (error, int) {
	intn := rand.Intn(9000) + 1000
	get := inits.Rdb.Get(inits.Ctx, "sendSmsCount")
	if get.Val() > "3" {
		return errors.New("发送短信一分钟只能发三次"), 0
	}

	sms := pkg.HuYiWuXian{}
	_, err := sms.Send(in.Mobile, strconv.Itoa(intn))
	if err != nil {
		return err, 0
	}
	inits.Rdb.Set(inits.Ctx, "sendSms"+in.Mobile, intn, time.Minute*5)
	inits.Rdb.Incr(inits.Ctx, "sendSmsCount")
	inits.Rdb.Expire(inits.Ctx, "sendSmsCount", time.Minute*10)
	return nil, intn
}
